import org.junit.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

/*
       BigInteger 可以处理非常大的整数，且不会溢出
 */
public class BigTest {
    public static void main(String[] args) {

    }

    @Test
    public void test1() {
        //创建方式
        BigInteger bigInt = new BigInteger("1243445443543543344535");
        System.out.println(bigInt.toString());

        byte[] bytes = {0x12, 0x34, 0x56, 0x78};
        BigInteger bigInt1 = new BigInteger(bytes);
        System.out.println(bigInt1);

        long l1 = 123434345343555334L;
        BigInteger bigInt2 = BigInteger.valueOf(l1);

        //基本运算，加减乘除和取模
        BigInteger bigInt3 = bigInt1.add(bigInt2);
        System.out.println(bigInt3);
        BigInteger bigInt4 = bigInt1.subtract(bigInt2);
        System.out.println(bigInt4);
        BigInteger bigInt5 = bigInt1.multiply(bigInt2);
        System.out.println(bigInt5);
        BigInteger bigInt6 = bigInt1.divide(bigInt2);
        System.out.println(bigInt6);
        BigInteger bigInt7 = bigInt1.mod(bigInt2);
        System.out.println(bigInt7);

        //比较大小，返回
        int comp = bigInt1.compareTo(bigInt2);
        System.out.println(comp);
        System.out.println(comp > 0 ? "大于" : (comp == 0 ? "相等" : "小于"));

        System.out.println(bigInt1.equals(bigInt2));

        //位运算
        BigInteger bigInt8 = bigInt1.shiftRight(1);
        System.out.println(bigInt8);
        BigInteger bigInt9 = bigInt1.shiftLeft(1);
        System.out.println(bigInt9);

        //数学运算
        System.out.println(bigInt1.abs()); //绝对值
        System.out.println(bigInt1.pow(2)); //2次方
        System.out.println(bigInt1.gcd(bigInt2)); //最大公约数
        System.out.println(bigInt1.isProbablePrime(100)); //是否是素数，参数是确定性大小

        //转换
        int intValue = bigInt1.intValue();
        long longValue = bigInt1.longValue();
        String strValue = bigInt1.toString();

    }

    @Test
    public void test2() {
        BigDecimal a = new BigDecimal("123.4354353");
        BigDecimal b = BigDecimal.valueOf(124.3435353);
        BigDecimal c = BigDecimal.valueOf(125L);

        BigDecimal sum = a.add(b);
        BigDecimal difference = a.subtract(b);
        BigDecimal product = a.multiply(b);
        /*
        BigDecimal 的除法运算需要指定舍入模式（RoundingMode），以避免无限小数的情况。常见的舍入模式有：
            RoundingMode.UP：向远离零的方向舍入。
            RoundingMode.DOWN：向接近零的方向舍入。
            RoundingMode.CEILING：向正无穷方向舍入。
            RoundingMode.FLOOR：向负无穷方向舍入。
            RoundingMode.HALF_UP：四舍五入。
            RoundingMode.HALF_DOWN：五舍六入。
            RoundingMode.HALF_EVEN：银行家舍入法（四舍六入，五取偶）
         */
        BigDecimal quotient = a.divide(b, RoundingMode.UP);

        System.out.println(a.compareTo(b));
        System.out.println(a.equals(b));

        //设置精度
        BigDecimal newBig = a.setScale(2, RoundingMode.DOWN);

        //其他数学方法
        System.out.println(a.abs());
        System.out.println(a.negate());  //相反数
        System.out.println(a.pow(3));  //三次方
        System.out.println(a.movePointLeft(1)); //小数点左移一位
        System.out.println(a.movePointRight(1)); //小数点右移一位

        //转换,可能存在精度丢失
        System.out.println(a.doubleValue());
        System.out.println(a.intValue());
        System.out.println(a.longValue());
        System.out.println(a.toString());

    }
}

